<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="发送方Id" prop="sendId">
      <el-input v-model="dataForm.sendId" placeholder="发送方Id"></el-input>
    </el-form-item>
    <el-form-item label="接收方Id" prop="receiveId">
      <el-input v-model="dataForm.receiveId" placeholder="接收方Id"></el-input>
    </el-form-item>
    <el-form-item label="发送方类型" prop="sendType">
      <el-input v-model="dataForm.sendType" placeholder="发送方类型"></el-input>
    </el-form-item>
    <el-form-item label="接收方类型" prop="receiveType">
      <el-input v-model="dataForm.receiveType" placeholder="接收方类型"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="发送时间" prop="sendTime">
      <el-input v-model="dataForm.sendTime" placeholder="发送时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          messageId: 0,
          sendId: '',
          receiveId: '',
          sendType: '',
          receiveType: '',
          content: '',
          sendTime: ''
        },
        dataRule: {
          sendId: [
            { required: true, message: '发送方Id不能为空', trigger: 'blur' }
          ],
          receiveId: [
            { required: true, message: '接收方Id不能为空', trigger: 'blur' }
          ],
          sendType: [
            { required: true, message: '发送方类型不能为空', trigger: 'blur' }
          ],
          receiveType: [
            { required: true, message: '接收方类型不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          sendTime: [
            { required: true, message: '发送时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.messageId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.messageId) {
            this.$http({
              url: this.$http.adornUrl(`/message/message/info/${this.dataForm.messageId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sendId = data.message.sendId
                this.dataForm.receiveId = data.message.receiveId
                this.dataForm.sendType = data.message.sendType
                this.dataForm.receiveType = data.message.receiveType
                this.dataForm.content = data.message.content
                this.dataForm.sendTime = data.message.sendTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/message/message/${!this.dataForm.messageId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'messageId': this.dataForm.messageId || undefined,
                'sendId': this.dataForm.sendId,
                'receiveId': this.dataForm.receiveId,
                'sendType': this.dataForm.sendType,
                'receiveType': this.dataForm.receiveType,
                'content': this.dataForm.content,
                'sendTime': this.dataForm.sendTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
